package com.baicaoyuan.web.service;

import com.baicaoyuan.web.common.StringUtils;
import com.baicaoyuan.web.common.TimeUtils;
import com.baicaoyuan.web.enums.RequirementStatusEnum;
import com.baicaoyuan.web.enums.StatusEnum;
import com.baicaoyuan.web.model.RequirementDO;
import com.baicaoyuan.web.model.UserDO;
import com.baicaoyuan.web.repository.RequirementRepository;
import com.baicaoyuan.web.repository.UserRepository;
import com.baicaoyuan.web.request.br.BRequirementRequest;
import com.baicaoyuan.web.response.RequirementPersonResponse;
import com.baicaoyuan.web.response.RequirementResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Service
public class RequirementService {

    @Autowired
    RequirementRepository requirementRepository;

    @Autowired
    UserRepository userRepository;

    public void save(BRequirementRequest request, String category) {
        RequirementDO requirement = new RequirementDO();
        BeanUtils.copyProperties(request, requirement);
        requirement.setCreateTime(TimeUtils.getCurrentTime());
        requirement.setUserFk(request.getUserId());
        UserDO user = userRepository.getOne(request.getUserId());
        if (user != null) {
            requirement.setUserName(user.getUserName());
            requirement.setTelephone(user.getTelephone());
        }
        requirement.setCategory(category);
        requirement.setStatus(RequirementStatusEnum.NORMAL.getStatus());
        Objects.requireNonNull(this.requirementRepository.save(requirement));
    }

    public List<RequirementResponse> getRequirement(String telephone, String category, Integer page, Integer size) {
        List<RequirementDO> list = this.requirementRepository.getRequirements(StringUtils.plusPercent(telephone), category, (page - 1) * size, size);
        List<RequirementResponse> result = new ArrayList<>();
        list.forEach(x -> {
            RequirementResponse r = new RequirementResponse();
            BeanUtils.copyProperties(x, r);
            result.add(r);
        });

        return result;
    }


    /**
     * 获取需求或者反馈
     *
     * @param userId
     * @return
     */
    public List<RequirementPersonResponse> getRequirementOrFeedback(Long userId, String category, Integer page, Integer size) {

        List<RequirementDO> list = this.requirementRepository.findByUserFkAndCategory(userId, category, new PageRequest(page - 1, size));

        List<RequirementPersonResponse> result = new ArrayList<>();
        list.forEach(x -> {
            RequirementPersonResponse r = new RequirementPersonResponse();
            BeanUtils.copyProperties(x, r);
            result.add(r);
        });

        return result;
    }

    public Integer getCountOfAll(String telephone, String category) {
        return this.requirementRepository.countAllByStatusIsNotAndTelephoneLikeAndCategory(StatusEnum.DELETE.getStatus(), StringUtils.plusPercent(telephone), category);
    }

    public void delete(Long id) {
        Integer i = this.requirementRepository.updateStatus(id, RequirementStatusEnum.DELETE.getStatus());
        if (i == 0) {
            throw new RuntimeException();
        }
    }

    public void check(Long id) {
        Integer i = this.requirementRepository.updateStatus(id, RequirementStatusEnum.DO.getStatus());
        if (i == 0) {
            throw new RuntimeException();
        }
    }
}

